package com.vincce.cyicai.backstage.util;

/*import com.vincce.kanglianwx.core.constant.ApiConstant;
import com.vincce.kanglianwx.mgt.entity.WeiXinTemplate;*/

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * User: xdy
 * Date: 12/12/13
 * Time: 5:45 PM
 * To change this template use File | Settings | File Templates.
 */
public class EmailUtil {
    private static final String sender = "yk731340933@163.com";
    private static final String password = "731340933";

    /**
     * 发送邮件
     * @param receivers 接受者邮箱
     * @param subject 邮件标题
     * @param mailContent 邮件内容
     */
    public static void sendEmail( String[] receivers, String subject, String mailContent) {
        Properties props = new Properties();
        //设置smtp服务器地址
        //这里使用QQ邮箱，记得关闭独立密码保护功能和在邮箱中设置POP3/IMAP/SMTP服务
        props.put("mail.smtp.host", "smtp.163.com");
        props.put("mail.smtp.port","25");
        /*props.put("mail.smtp.host", "smtp.bocommlife.com");
        props.put("mail.smtp.port","25");*/
        //需要验证
        props.put("mail.smtp.auth", "true");
       /* props.put("mail.smtp.auth", "false");*/
        //创建验证器
        Authenticator authenticator = new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(sender, password);
            }
        };
        //使用Properties创建Session
        Session session = Session.getDefaultInstance(props, authenticator);
        //Set the debug setting for this Session
        session.setDebug(true);
        try {
            // 根据session创建一个邮件消息
            MimeMessage mailMessage = new MimeMessage(session);
            // 创建邮件发送者地址
            Address from = new InternetAddress(sender);
            // 设置邮件消息的发送者
            mailMessage.setFrom(from);
            // 创建邮件的接收者地址，并设置到邮件消息中
            //Address to = new InternetAddress(receivers);
            //mailMessage.setRecipient(Message.RecipientType.TO,to);
            InternetAddress[] receiver = new InternetAddress[receivers.length];
            for (int i=0; i<receivers.length; i++) {
                receiver[i] = new InternetAddress(receivers[i]);
            }
            //设置收件人邮件
            mailMessage.setRecipients(Message.RecipientType.TO, receiver);
            // 设置邮件消息的主题
            //mailMessage.setSubject(subject);
            mailMessage.setSubject(subject, "utf-8");

            // 设置邮件消息发送的时间
            mailMessage.setSentDate(new Date());
            // 设置邮件消息的主要内容
            //String mailContent = mailInfo.getContent();
            // MiniMultipart类是一个容器类，包含MimeBodyPart类型的对象
            Multipart mainPart = new MimeMultipart();
            // 创建一个包含HTML内容的MimeBodyPart
            BodyPart html = new MimeBodyPart();
            // 设置HTML内容
            html.setContent(mailContent, "text/html; charset=utf-8");
            mainPart.addBodyPart(html);
            // 将MiniMultipart对象设置为邮件内容
            mailMessage.setContent(mainPart);
            //mailMessage.setText(mailContent);
            //mailMessage.setContent(mailContent,"utf-8");
            // 发送邮件
            Transport.send(mailMessage);
        } catch (MessagingException ex) {
            ex.printStackTrace();
        }

    }


    public static void main(String [] arg){
        System.out.print("AA");
        String [] receivers={"731340933@qq.com","1317422922@qq.com"};
        String  subject ="邮件发送测试";
        String mailContent="测试类容";
        sendEmail(receivers,subject,mailContent);
    }

}



